import { v3 } from 'cc';
import { log } from 'cc';
import { Quat } from 'cc';
import { quat } from 'cc';
import { Vec3 } from 'cc';
import { math } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { linear } from '../../../scripts/libs/utils/math';
import { TweenToStart } from '../../../scripts/libs/rxjs/cc3/TweenStart';
import { Sprite } from 'cc';
import { Color } from 'cc';
import { color } from 'cc';
import { MotionStreak } from 'cc';
const { ccclass, property } = _decorator;

const _q0 = math.quat();
const _v30 = v3();
const _v31 = v3();

// 蓝色 00D6FF
// 黄色 FFCC00
// 紫色 8500FF

@ccclass('Caidai')
export class Caidai extends Component {

    @property(Node)
    caidaiNode: Node = null;

    @property(Node)
    tailNode: Node = null;

    _lastPos: Vec3 = v3();

    _t: number = 0;

    _forwardMulti: number = -20;

    _var0: number = .4;
    _var1: number = 1;
    _willOpenTail = false;


    private _color: Color = color();
    public get color(): Color { return this._color; }
    public set color(v: Color) {
        this._color = v;
        this.refreshColor();
    }

    endCallback: () => void = null;

    start() {
        // this._t = Math.random() * 10;
        this.init();
    }

    init() {
        this._t = Math.random() * 10;
        this.node.setRotationFromEuler(90, 360 * Math.random(), Math.random() * 40 - 20);
        this._forwardMulti = linear(0, 1, -30, -20, Math.random());
        this._var0 = linear(0, 1, .3, .5, Math.random());
        this._var1 = linear(0, 1, .9, 1.1, Math.random());
        this.node.setScale(0, 0, 0);
        TweenToStart(this.node, .1, { scale: v3(.6, .8, .8) }, { easing: 'expoOut' }).subscribe().addTo(this.node);
        if (this.tailNode != null) this.tailNode.active = false;
        this._willOpenTail = Math.random() > .9;
    }

    refreshColor() {
        if (this.caidaiNode != null) this.caidaiNode.getComponent(Sprite).color = this.color;
        if (this.tailNode != null) this.tailNode.getComponent(MotionStreak).color = this.color;
    }

    update(deltaTime: number) {
        this._t += deltaTime;
        if (this.caidaiNode != null) {
            this.caidaiNode.setRotationFromEuler(this._t * 800, 0, 0);
        }

        Quat.fromAxisAngle(_q0, v3(this._var0, -.1, 1), deltaTime * this._var1);

        _v31.set(this.node.forward);
        _v31.multiplyScalar(this._forwardMulti);
        this.node.getWorldPosition(_v30);
        _v30.add(_v31)

        _v30.add(this._velocity);

        _v30.z = Math.min(100, _v30.z);
        this.node.setWorldPosition(_v30);

        this.node.rotate(_q0);

        if (this.node.worldPosition.y < -500) {
            if (this.endCallback) this.endCallback();
            else this.node.destroy();
        }

        this._velocity.multiplyScalar(.95);

        if (this._velocity.lengthSqr() < 40) {
            if (this._willOpenTail) {
                if (this.tailNode != null) this.tailNode.active = true;
            }
        }
    }

    _velocity = v3();
    burst(targetpos: Vec3, distance: number) {
        _v30.set(targetpos);
        _v30.normalize();
        let scalar = linear(0, 400, 0, 40, distance);
        // let scalar = Math.random() * 50;
        _v30.multiplyScalar(scalar);
        // log('scalar', scalar);
        this._velocity.set(_v30);
    }


}

